package com.plian.system.controller.qg.guarantee;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.qg.guarantee.PropertyGuaranteeRepayment;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.qg.guarantee.IPropertyGuaranteeRepaymentService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.qg.guarantee.PropertyGuaranteeRepaymentVO;
import com.plian.system.wrapper.qg.guarantee.PropertyGuaranteeRepaymentWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 企业担保还款 控制器
 *
 * @author jianglei
 * @since 2020-09-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/propertyguaranteerepayment")
public class PropertyGuaranteeRepaymentController extends BaseController {

    private IPropertyGuaranteeRepaymentService propertyGuaranteeRepaymentService;

    private PropertyGuaranteeRepaymentWrapper propertyGuaranteeRepaymentWrapper;

    private FormWorkflowService formWorkflowService;

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<PropertyGuaranteeRepaymentVO> detail(@Valid @RequestBody PropertyGuaranteeRepayment propertyGuaranteeRepayment) {
        PropertyGuaranteeRepayment detail = propertyGuaranteeRepaymentService.getOne(Condition.getQueryWrapper(propertyGuaranteeRepayment));
        PropertyGuaranteeRepaymentVO propertyGuaranteeExecuteVO = propertyGuaranteeRepaymentWrapper.entityToVO(detail);
        R<PropertyGuaranteeRepaymentVO> obj = new R<>(CommonCode.SUCCESS);
        obj.setPageData(propertyGuaranteeExecuteVO);
        return obj;
    }

    /**
     * 分页 企业担保还款
     */
    @PostMapping("/list")
    public ListResult<PropertyGuaranteeRepaymentVO> list(@Valid @RequestBody Query query) {
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time, execute_id");
        }
        List<SearchEntity> entityList = query.getSearch();
        QueryWrapper<PropertyGuaranteeRepayment> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new PropertyGuaranteeRepayment()), entityList
        );
        TokenSearch<PropertyGuaranteeRepayment> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);

        IPage<PropertyGuaranteeRepayment> pages = propertyGuaranteeRepaymentService.page(Condition.getPage(query), queryWrapper);
        List<PropertyGuaranteeRepaymentVO> propertyGuaranteeExecuteVOS = propertyGuaranteeRepaymentWrapper.entityToVO((pages).getRecords());
        ListResult<PropertyGuaranteeRepaymentVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(propertyGuaranteeExecuteVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

    /**
     * 新增或修改 企业担保还款
     */
    @PostMapping("/submit")
    @LogOperation("企业担保还款-提交")
    @Resubmit(delaySeconds = 0)
    public ObjResult submit(@Valid @RequestBody PropertyGuaranteeRepayment propertyGuaranteeRepayment) {
        ObjResult objResult = propertyGuaranteeRepaymentService.submit(propertyGuaranteeRepayment)
                ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
        HashMap<String, Object> processVariable = new HashMap<>();
        formWorkflowService.startProcessAndSimulate(String.valueOf(propertyGuaranteeRepayment.getId()), propertyGuaranteeRepayment.getFormType(), processVariable);

        return objResult;
    }

    /**
     * 删除 企业担保还款
     */
    @PostMapping("/remove")
    @LogOperation("企业担保还款-删除")
    public ObjResult remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return propertyGuaranteeRepaymentService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
    }

}
